{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "class BinaryNode:\n",
    "    indent = \"  \"\n",
    "\n",
    "    def __init__(self, value) -> None:\n",
    "        self.value = value\n",
    "        self.left_child = None\n",
    "        self.right_child = None\n",
    "\n",
    "    def add_left(self, new_node):\n",
    "        self.left_child = new_node\n",
    "\n",
    "    def add_right(self, new_node):\n",
    "        self.right_child = new_node\n",
    "\n",
    "    def __str__(self, level = 0):\n",
    "        result = f'{self.value}:\\n'\n",
    "        level += 1\n",
    "        \n",
    "        if (self.left_child == None and self.right_child == None):\n",
    "            return result\n",
    "\n",
    "        if (self.left_child == None):\n",
    "            result += f'{self.indent * level}None\\n'\n",
    "        else:\n",
    "            result += f'{self.indent * level}{self.left_child.__str__(level)}'\n",
    "\n",
    "        if (self.right_child == None):\n",
    "            result += f'{self.indent * level}None\\n'\n",
    "        else:\n",
    "            result += f'{self.indent * level}{self.right_child.__str__(level)}'\n",
    "            \n",
    "        return result\n",
    "\n",
    "    # Exhaustive Search\n",
    "    def find_node(self, target):\n",
    "        # If this node contains the target value, return self.\n",
    "        if self.value == target:\n",
    "            return self\n",
    "\n",
    "        # If the left child is not None:\n",
    "        if self.left_child != None:\n",
    "            result = self.left_child.find_node(target)\n",
    "            if result != None:\n",
    "                return result            \n",
    "\n",
    "        # If the right child is not None:\n",
    "        if self.right_child != None:\n",
    "            result = self.right_child.find_node(target)\n",
    "            if result != None:\n",
    "                return result \n",
    "        \n",
    "        return None\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build a test tree.\n",
    "#      Root\n",
    "#      /  \\\n",
    "#     A    B\n",
    "#    / \\    \\\n",
    "#   C   D    E\n",
    "#           /\n",
    "#          F\n",
    "root_node = BinaryNode(\"Root\")\n",
    "a_node = BinaryNode(\"A\")\n",
    "b_node = BinaryNode(\"B\")\n",
    "c_node = BinaryNode(\"C\")\n",
    "d_node = BinaryNode(\"D\")\n",
    "e_node = BinaryNode(\"E\")\n",
    "f_node = BinaryNode(\"F\")\n",
    "\n",
    "root_node.add_left(a_node)\n",
    "root_node.add_right(b_node)\n",
    "a_node.add_left(c_node)\n",
    "a_node.add_right(d_node)\n",
    "b_node.add_right(e_node)\n",
    "e_node.add_left(f_node)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_value(root, target):\n",
    "    node = root.find_node(target)\n",
    "    if node == None:\n",
    "        print(f'Value {target} not found')\n",
    "    else:\n",
    "        print(f'Found {node.value}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found Root\n",
      "Found E\n",
      "Found F\n",
      "Value Q not found\n",
      "Found F\n"
     ]
    }
   ],
   "source": [
    "# Find some values.\n",
    "find_value(root_node, 'Root')\n",
    "find_value(root_node, 'E')\n",
    "find_value(root_node, 'F')\n",
    "find_value(root_node, 'Q')\n",
    "\n",
    "# Find F in the B subtree.\n",
    "find_value(b_node, 'F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e63faaf9bc27d837377622cad56f3d439b9a97e8fa476559128293c868ce68e0"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('word_database': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
